package com.codedancer.campus.manager.schedule;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.json.JSONUtil;
import com.codedancer.campus.common.utils.UUIDGenerator;
import com.codedancer.campus.enums.*;
import com.codedancer.campus.manager.mq.producer.MqSendMessageProducer;
import com.codedancer.campus.manager.service.model.SystemMessageMqParamModel;
import com.codedancer.campus.mapper.*;
import com.codedancer.campus.po.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 校园生活自动审核
 * 主要对闲置商品、表白墙、失物招领、圈子、动态进行定时审核
 */
@Component
@Slf4j
public class SchoolLifeAutoExamineSchedule {

    @Autowired
    private CommodityMapper commodityMapper;
    @Autowired
    private CommodityExamineRecordMapper commodityExamineRecordMapper;
    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private ConfessionalWallMapper confessionalWallMapper;
    @Autowired
    private ConfessionalWallExamineRecordMapper confessionalWallExamineRecordMapper;
    @Autowired
    private LostFoundItemMapper lostFoundItemMapper;
    @Autowired
    private LostFoundItemExamineRecordMapper lostFoundItemExamineRecordMapper;
    @Autowired
    private DynamicMapper dynamicMapper;
    @Autowired
    private DynamicExamineRecordMapper dynamicExamineRecordMapper;
    @Autowired
    private MqSendMessageProducer mqSendMessageProducer;

    /**
     * 每隔10分钟跑一次
     */
    @Scheduled(cron = "0 */5 * * * ?")
    public void autoExamine() {
        // 获取超级管理员信息
        Admin admin = adminMapper.selectSuperAdmin();
        // 自动审核闲置商品
        autoExamineCommodity(admin.getUuid());
        // 自动审核表白墙
        autoExamineConfessionalWall(admin.getUuid());
        // 自动审核失物招领
        autoExamineLostFoundItem(admin.getUuid());
    }

    /**
     * 自动审核闲置商品
     */
    private void autoExamineCommodity(String adminUuid) {
        log.info("自动审核闲置商品任务开始");
        // 查询待审核的闲置商品
        List<Commodity> commodityList = commodityMapper.selectListByColumn("status", CommodityStatus.PENDING_REVIEW.getValue());
        if (CollUtil.isEmpty(commodityList)) {
            log.info("自动审核闲置商品任务结束");
            return;
        }
        for (Commodity commodity : commodityList) {
            // 判断是否审核超过5次
            if (commodity.examineCountIsGtFive()) {
                // 超过5次审核，直接跳过
                // TODO 做一个消息提示
                continue;
            }
            // 审核次数加1
            commodity.setExamineCount(commodity.getExamineCount() + 1);
            commodity.setStatus(CommodityStatus.ON_SHELF);
            commodityMapper.updateById(commodity);
            // 保存审核记录信息
            CommodityExamineRecord commodityExamineRecord = CommodityExamineRecord.builder()
                .uuid(UUIDGenerator.getUUID())
                .commodityUuid(commodity.getUuid())
                .examineAdminUuid(adminUuid)
                .isPass(true).build();
            commodityExamineRecordMapper.insert(commodityExamineRecord);
            // 发送消息
            SystemMessageMqParamModel systemMessageMqParamModel = SystemMessageMqParamModel.builder()
                .messageType(SystemMessageType.COMMODITY_EXAMINE)
                .content("【" + commodity.getTitle() + "】审核通过")
                .receiveUserUuid(commodity.getPublishUserUuid()).build();
            mqSendMessageProducer.sendSystemMessage(JSONUtil.toJsonStr(systemMessageMqParamModel));
            // 给粉丝推送消息
            // 根据用户uuid获取全部关注者记录
            SystemMessageMqParamModel commodityPublishSystemMessageMqParamModel = SystemMessageMqParamModel.builder()
                .commodityPublishUserUuid(commodity.getPublishUserUuid())
                .messageType(SystemMessageType.COMMODITY_PUBLISH).build();
            mqSendMessageProducer.sendSystemMessage(
                JSONUtil.toJsonStr(commodityPublishSystemMessageMqParamModel));
        }
        log.info("自动审核闲置商品任务结束");
    }

    /**
     * 自动审核表白墙
     */
    private void autoExamineConfessionalWall(String adminUuid) {
        log.info("自动审核表白墙任务开始");
        // 查询待审核表白墙
        List<ConfessionalWall> confessionalWalls = confessionalWallMapper.selectListByColumn("status", ConfessionalWallStatus.PENDING_REVIEW.getValue());
        if (CollUtil.isEmpty(confessionalWalls)) {
            log.info("自动审核表白墙任务结束");
            return;
        }
        for (ConfessionalWall confessionalWall : confessionalWalls) {
            if (confessionalWall.examineCountIsGtFive()) {
                // 超过5次审核，直接跳过
                // TODO 做一个消息提示
                continue;
            }
            confessionalWall.setExamineCount(confessionalWall.getExamineCount() + 1);
            confessionalWall.setStatus(ConfessionalWallStatus.PASS);
            confessionalWallMapper.updateById(confessionalWall);
            // 保存审核记录信息
            ConfessionalWallExamineRecord confessionalWallExamineRecord = ConfessionalWallExamineRecord.builder()
                .uuid(UUIDGenerator.getUUID())
                .confessionalWallUuid(confessionalWall.getUuid())
                .examineAdminUuid(adminUuid)
                .isPass(true)
                .build();
            confessionalWallExamineRecordMapper.insert(confessionalWallExamineRecord);
            // 发送消息
            SystemMessageMqParamModel systemMessageMqParamModel = SystemMessageMqParamModel.builder()
                .messageType(SystemMessageType.COMMODITY_EXAMINE)
                .content("【" + confessionalWall.getContent() + "】审核通过")
                .receiveUserUuid(confessionalWall.getPublishUserUuid()).build();
            mqSendMessageProducer.sendSystemMessage(JSONUtil.toJsonStr(systemMessageMqParamModel));
        }
        log.info("自动审核表白墙任务结束");
    }

    /**
     * 自动审核失物招领
     *
     * @param adminUuid
     */
    private void autoExamineLostFoundItem(String adminUuid) {
        log.info("自动审核表白墙任务开始");
        // 查询待审核失物招领
        List<LostFoundItem> lostFoundItems = lostFoundItemMapper.selectListByColumn("status", LostFoundItemStatus.PENDING_REVIEW.getValue());
        if (CollUtil.isEmpty(lostFoundItems)) {
            log.info("自动审核表白墙任务结束");
            return;
        }
        for (LostFoundItem lostFoundItem : lostFoundItems) {
            if (lostFoundItem.examineCountIsGtFive()) {
                // 超过5次审核，直接跳过
                // TODO 做一个消息提示
                continue;
            }
            lostFoundItem.setExamineCount(lostFoundItem.getExamineCount() + 1);
            // 判断物品发布类型
            if (lostFoundItem.getType().equals(LostFoundItemType.PICK_UP)) {
                lostFoundItem.setStatus(LostFoundItemStatus.NO_CLAIM);
            }
            if (lostFoundItem.getType().equals(LostFoundItemType.LOST)) {
                lostFoundItem.setStatus(LostFoundItemStatus.NO_FOUND);
            }
            lostFoundItemMapper.updateById(lostFoundItem);
            // 保存审核记录
            LostFoundItemExamineRecord lostFoundItemExamineRecord = LostFoundItemExamineRecord.builder()
                .uuid(UUIDGenerator.getUUID())
                .lostFoundItemUuid(lostFoundItem.getUuid())
                .examineAdminUuid(adminUuid)
                .isPass(true).build();
            lostFoundItemExamineRecordMapper.insert(lostFoundItemExamineRecord);
            // 发送消息
            SystemMessageMqParamModel systemMessageMqParamModel = SystemMessageMqParamModel.builder()
                .messageType(SystemMessageType.COMMODITY_EXAMINE)
                .content("【" + lostFoundItem.getName() + "】审核通过")
                .receiveUserUuid(lostFoundItem.getPublishUserUuid()).build();
            mqSendMessageProducer.sendSystemMessage(JSONUtil.toJsonStr(systemMessageMqParamModel));
            // 给粉丝推送消息
            SystemMessageMqParamModel lostFoundItemPublishSystemMessageMqParamModel = SystemMessageMqParamModel.builder()
                .lostFoundItemPublishUserUuid(lostFoundItem.getPublishUserUuid())
                .messageType(SystemMessageType.LOST_FOUND_ITEM_PUBLISH).build();
            mqSendMessageProducer.sendSystemMessage(
                JSONUtil.toJsonStr(lostFoundItemPublishSystemMessageMqParamModel));
        }
        log.info("自动审核表白墙任务结束");
    }

    /**
     * 自动审核动态
     *
     * @param adminUuid
     */
    private void autoExamineDynamic(String adminUuid) {
        log.info("自动审核动态任务开始");
        // 查询全部待审核动态
        List<Dynamic> dynamicList = dynamicMapper.selectListByColumn("status", DynamicStatus.PENDING_REVIEW.getValue());
        if (CollUtil.isEmpty(dynamicList)) {
            log.info("自动审核动态任务结束");
            return;
        }
        for (Dynamic dynamic : dynamicList) {
            if (dynamic.examineCountIsGtFive()) {
                // 超过5次审核，直接跳过
                // TODO 做一个消息提示
                continue;
            }
            dynamic.setStatus(DynamicStatus.PASSED);
            dynamic.setExamineCount(dynamic.getExamineCount() + 1);
            dynamicMapper.updateById(dynamic);
            // 保存审核记录
            DynamicExamineRecord dynamicExamineRecord = DynamicExamineRecord.builder()
                .uuid(UUIDGenerator.getUUID())
                .examineAdminUuid(adminUuid)
                .dynamicUuid(dynamic.getUuid())
                .isPass(true)
                .build();
            dynamicExamineRecordMapper.insert(dynamicExamineRecord);
            // 发送消息
            SystemMessageMqParamModel systemMessageMqParamModel = SystemMessageMqParamModel.builder()
                .messageType(SystemMessageType.COMMODITY_EXAMINE)
                .content("【" + dynamic.getContent() + "】审核通过")
                .receiveUserUuid(dynamic.getPublishUserUuid()).build();
            mqSendMessageProducer.sendSystemMessage(JSONUtil.toJsonStr(systemMessageMqParamModel));
            // 给粉丝推送消息
            SystemMessageMqParamModel dynamicSystemMessageMqParamModel = SystemMessageMqParamModel
                .builder()
                .messageType(SystemMessageType.DYNAMIC_PUBLISH)
                .dynamicPublishUserUuid(dynamic.getPublishUserUuid()).build();
            mqSendMessageProducer.sendSystemMessage(JSONUtil.toJsonStr(dynamicSystemMessageMqParamModel));
        }
    }
}
